/********************************************************************************
 * Project Name		[JavaEE_Web]
 * File Name     	[DaoUtils.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package mvc.utils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

/**
 * 一个负责基本CRUD的Dao<br/>
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class DaoUtils<T> {

	private QueryRunner queryRunner = new QueryRunner();

	private Class<T> classType;

	public DaoUtils() {
		classType = ReflectionUtils.getSuperClassGenericType(getClass());
	}

	/**
	 * 查询所有记录
	 * 
	 * @param sql
	 * @param parameters
	 * @return
	 */
	public List<T> getAll(String sql, Object... parameters) {
		Connection connection = null;

		try {
			connection = JdbcUtils.getInstance().getConnection();
			return queryRunner.query(connection, sql, new BeanListHandler<>(classType), parameters);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				JdbcUtils.getInstance().releaseConnection(connection);
			} catch (SQLException e) {
			}
		}
		return null;
	}

	/**
	 * 查询一条记录
	 * 
	 * @param sql
	 * @param parameters
	 * @return
	 */
	public T uniqueResult(String sql, Object... parameters) {
		Connection connection = null;

		try {
			connection = JdbcUtils.getInstance().getConnection();
			return queryRunner.query(connection, sql, new BeanHandler<>(classType), parameters);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				JdbcUtils.getInstance().releaseConnection(connection);
			} catch (SQLException e) {
			}
		}
		return null;
	}

	/**
	 * 保存, 更新, 删除记录
	 * 
	 * @param sql
	 * @param parameters
	 */
	public void update(String sql, Object... parameters) {
		Connection connection = null;

		try {
			connection = JdbcUtils.getInstance().getConnection();
			queryRunner.update(connection, sql, parameters);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				JdbcUtils.getInstance().releaseConnection(connection);
			} catch (SQLException e) {
			}
		}
	}

	/**
	 * 返回某一个字段的值
	 * 
	 * @param sql
	 * @param parameters
	 * @return
	 */
	public <C> C getSingleColumn(String sql, Object... parameters) {
		Connection connection = null;

		try {
			connection = JdbcUtils.getInstance().getConnection();
			return queryRunner.query(connection, sql, new ScalarHandler<>(), parameters);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				JdbcUtils.getInstance().releaseConnection(connection);
			} catch (SQLException e) {
			}
		}
		return null;
	}
}
